<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <h2>系统登录</h2>
      </div>

      <el-form
          :model="loginForm"
          class="login-form"
          @keyup.enter="handleLogin"
      >
        <el-form-item prop="account">
          <el-input
              v-model="loginForm.account"
              placeholder="请输入用户账号"
              prefix-icon="User"
              clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
              clearable
          />
        </el-form-item>

        <el-form-item v-if="showCaptcha" prop="captcha">
          <div class="captcha-container">
            <el-input
                v-model="loginForm.captcha"
                placeholder="请输入验证码"
                prefix-icon="Key"
                class="captcha-input"
            />
            <div class="captcha-image" @click="refreshCaptcha">
              <img :src="captchaUrl" alt="验证码">
            </div>
          </div>
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <el-link type="primary" :underline="false" class="forget-pwd">忘记密码?</el-link>
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <el-divider>其他登录方式</el-divider>
        <div class="social-login">
          <el-icon class="social-icon"><svg-icon icon-class="wechat" /></el-icon>
          <el-icon class="social-icon"><svg-icon icon-class="alipay" /></el-icon>
          <el-icon class="social-icon"><svg-icon icon-class="qq" /></el-icon>
        </div>
        <div class="register-link">
          还没有账号? <el-link type="primary" :underline="false">立即注册</el-link>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {ref, reactive, onMounted} from 'vue'
import { ElMessage } from 'element-plus'

const loading = ref(false)
const showCaptcha = ref(false)
const captchaUrl = ref('')
const num = ref('')

const loginForm = reactive({
  account: '',
  password: '',
  captcha: '',
  num: '',
  rememberMe: false
})
// 刷新验证码
const refreshCaptcha = () => {
  axiosGet(`captcha?t=${new Date().getTime()}`)
      .then(ret=>{
        captchaUrl.value = ret.data
        num.value = ret.msg
      })
}

import {axiosGet, axiosPost, post} from "@/utils/axios-utils.js";
onMounted(()=>{
  // refreshCaptcha()
})
import {useUserStore} from "@/stores/index.js";
const userStore = useUserStore()
import {useRouter} from "vue-router";
const router = useRouter()
// 登录处理
const handleLogin = () => {
      // 这里替换为实际的登录API调用
  loginForm.num = num.value
  console.log(loginForm)
      post('auth/login', loginForm)
          .then(ret=>{
             if(ret.code == 'ok'){
               userStore.setUser(ret.data)
               ElMessage("登录成功")
               router.push('/order/list')
             }else{
               ElMessage.error("登录失败！");
             }
          });
      return false;
}

// 如果需要显示验证码
showCaptcha.value = true
// refreshCaptcha()
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
  //background-image: url('https://example.com/background.jpg');
  background-size: cover;
}

.login-card {
  width: 420px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 24px;
}

.login-header h2 {
  color: #333;
  font-weight: 500;
}

.login-form {
  margin: 0 20px;
}

.login-btn {
  width: 100%;
  height: 40px;
  margin-top: 10px;
}

.forget-pwd {
  float: right;
}

.captcha-container {
  display: flex;
  align-items: center;
}

.captcha-input {
  flex: 1;
  margin-right: 10px;
}

.captcha-image {
  width: 100px;
  height: 40px;
  cursor: pointer;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  overflow: hidden;
}

.captcha-image img {
  width: 100%;
  height: 100%;
}

.login-footer {
  margin-top: 20px;
  text-align: center;
}

.social-login {
  display: flex;
  justify-content: center;
  margin: 10px 0;
}

.social-icon {
  font-size: 24px;
  margin: 0 10px;
  cursor: pointer;
  color: #606266;
}

.social-icon:hover {
  color: #409eff;
}

.register-link {
  margin-top: 16px;
  font-size: 14px;
  color: #606266;
}
</style>